<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
            <a-row>
              <a-col :span="18">
            <a-form-model-item label="模板编号" prop="tmplCode" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.tmplCode" placeholder="请输入模板编号"/>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
                  <a-col :span="18">
            <a-form-model-item label="报告项目名称" prop="subItemName" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.subItemName"  disabled/>
            </a-form-model-item>
          </a-col>
            </a-row>
          <a-row>
              <a-col :span="18">
                  <a-form-model-item label="是否异常" prop="isAbnormal" :rules="$valid.required" :maxLength="50">
                      <a-select
                              v-model="formInfo.isAbnormal"
                              placeholder="请选择是否可用"
                      >
                          <a-select-option
                                  v-for="(item) in this.$sysDictOptions('whetherFlag')"
                                  :key="item.k"
                                  :value="parseInt(item.k)">
                              {{ item.v }}
                          </a-select-option>
                      </a-select>
                  </a-form-model-item>
              </a-col>
          </a-row>
          <a-row>
              <a-col :span="18">
                  <a-form-model-item label="是否默认值" prop="isDefault" :rules="$valid.required" :maxLength="50">
                      <a-select
                              v-model="formInfo.isDefault"
                              placeholder="请选择是否可用"
                      >
                          <a-select-option
                                  v-for="(item) in this.$sysDictOptions('whetherFlag')"
                                  :key="item.k"
                                  :value="parseInt(item.k)">
                              {{ item.v }}
                          </a-select-option>
                      </a-select>
                  </a-form-model-item>
              </a-col>
          </a-row>
                <a-row>
              <a-col :span="18">
            <a-form-model-item label="所见" prop="finding" :rules="$valid.required" :labelCol="{xs: { span: 18 },sm: { span: 6 }}"  :wrapperCol="{xs: { span: 18 },sm: { span: 16 }}">
              <a-input v-model="formInfo.finding" placeholder="请输入所见" type="textarea"/>
            </a-form-model-item>
          </a-col>
                </a-row>
          <a-row>
                  <a-col :span="18">
            <a-form-model-item label="小结" prop="summary" :labelCol="{xs: { span: 18 },sm: { span: 6 }}"  :wrapperCol="{xs: { span: 18 },sm: { span: 16 }}">
              <a-input v-model="formInfo.summary" placeholder="请输入小结" type="textarea"/>
            </a-form-model-item>
          </a-col>
            </a-row>

                <a-row>
              <a-col :span="18">
            <a-form-model-item label="排序" prop="orderNum" :maxLength="50">
              <a-input v-model="formInfo.orderNum" placeholder="请输入排序"/>
            </a-form-model-item>
          </a-col>
            </a-row>
          <a-row>
              <a-col :span="18">
                  <a-form-model-item label=""  :rules="$valid.required" :maxLength="50">
                      <a-checkbox style="margin-top: 13px" :checked="uniqueFlag" @change="onCheckBoxChange"/>是否排除报告项目下的其他异常
                  </a-form-model-item>
              </a-col>
          </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  export default {
    name: 'HealthSubItemRstTmplEdit',
    components: {
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
          uniqueFlag:false,
        formInfo: {
          tmplCode: undefined,
          parentCode: undefined,
          level: undefined,
          subItemCode: undefined,
          finding: undefined,
          summary: undefined,
          isAbnormal: undefined,
          isDefault: undefined,
          orderNum: undefined,
          createTime: undefined,
          departCode: undefined,
          remark: undefined,
          wx: '',
            subItemName:undefined
        },
        url: {
          saveInfo: '/system/healthSubItemRstTmpl/save',
          getInfo: '/system/healthSubItemRstTmpl/info'
        }
      }
    },
    created () {
    },
    methods: {
        onCheckBoxChange (e) {
            this.uniqueFlag = !this.uniqueFlag
        },
      add (subItem) {
            console.log("sss",subItem)
          this.formInfo.subItemCode = subItem.itemCode
          this.formInfo.subItemName = subItem.itemName
          this.formInfo.subItemId = subItem.id
          this.formInfo.isAbnormal=0
          this.formInfo.isDefault=0
          this.visible = true
      },
      edit (record) {
            console.log('aaa',record)
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
                if(this.formInfo.isUnique==1){
                    this.uniqueFlag=true
                }else{
                    this.uniqueFlag=false
                }
                this.formInfo.subItemName = record.subItemName
            })
          })
        }

        this.visible = true
      },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
          if(this.uniqueFlag){
              this.formInfo.isUnique=1
          }else{
              this.formInfo.isUnique=0
          }
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
          this.uniqueFlag=false
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      }
    }
  }
</script>
